import Line from "./Line.js";
import Area from "./Area.js";
import Height from "./Height.js";
export default class Measure {
  handler;
  collection;
  mode;
  result;
  constructor(viewer) {
    this.viewer = viewer;
    this.scene = this.viewer.scene;
    this.init();
  }
  init() {
    // 初始鼠标事件
    if (!this.handler) {
      this.handler = new Cesium.ScreenSpaceEventHandler(this.scene.canvas);
    }
    // 初始化集合
    if (!this.collection) {
      this.collection = new Cesium.CustomDataSource("MeasureCollection");
    }
    this.viewer.dataSources.add(this.collection);
  }
  // mode values
  static LINE = 0;
  static AREA = 1;
  static HEIGHT = 2;
  ChangeMode(val, cb) {
    // 重置实体集合以及点选事件
    this.#reset();
    // 更换测量模式
    this.mode = val;
    switch (this.mode) {
      case 0:
        Line(this.collection, this.handler, cb);
        break;
      case 1:
        Area(this.collection, this.handler, cb);
        break;
      case 2:
        Height(this.collection, this.handler, cb);
        break;
    }
  }

  // 重置鼠标事件和集合
  #reset() {
    // 清空实体
    this.collection && this.collection.entities.removeAll();
    // 移除鼠标事件
    this.handler &&
      this.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
    this.handler &&
      this.handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    this.handler &&
      this.handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
  }

  Clear() {
    this.#reset();
  }
}
